package map.android.baidu.carpool.data;

import android.net.Uri;
import android.text.TextUtils;
import com.baidu.components.api.extra.APIProxy;
import com.baidu.lbs.net.http.model.ComNetRequestParams;
import com.baidu.mapframework.api.ComAPIManager;
import map.android.baidu.carpool.utils.MD5Util;
import org.apache.http.message.BasicNameValuePair;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by mume on 15/3/23.
 * 拼车请求的参数
 */
public class CarpoolRequestParam extends ComNetRequestParams{
    private boolean mSig = false;

    public CarpoolRequestParam(Map<String, String> source, boolean sig) {
        super(source ,sig);
        this.mSig = sig;
    }

    @Override
    protected List<BasicNameValuePair> getParamsList() {

        List<BasicNameValuePair> lparams = super.getParamsList();
        if (mSig) {
            addSigParams(lparams);
        }
        return lparams;
    }

    @Override
    protected void addUniversalParams() {

    }

    private void addSigParams(List<BasicNameValuePair> lparams) {

        if (lparams == null) {
            return;
        }

//        BasicNameValuePair random = new BasicNameValuePair("random", "" + Calendar.getInstance().getTimeInMillis());
//        lparams.add(random);

        Collections.sort(lparams, new Comparator<BasicNameValuePair>() {

            @Override
            public int compare(BasicNameValuePair lhs, BasicNameValuePair rhs) {
                return lhs.getName().compareTo(rhs.getName());
            }
        });

        String key = "&token=baidu2015_carpo0420_^&*";
        String srcString = urlToString(lparams) + key;
        // if (srcString.matches("[\\d\\D]+&bduss=[^&]*&[\\d\\D]+")) {
        // srcString = srcString.replaceAll("&bduss=[^&]*&", "&");
        // } else if (srcString.matches("bduss=[^&]*&[\\d\\D]+")) {
        // srcString = srcString.replaceAll("bduss=[^&]*&", "");
        // } else if (srcString.matches("[\\d\\D]+&bduss=[^&]*")) {
        // srcString = srcString.replaceAll("&bduss=[^&]*", "");
        // } else if (srcString.matches("bduss=[^&]*")) {
        // srcString = "";
        // }
        String md5String = MD5Util.encode(srcString);
        lparams.add(new BasicNameValuePair("sign", md5String));
    }

    public static String signPartOfParams(Map<String , String> params){
        if(params == null) {
            return "";
        }
        LinkedList signParams = new LinkedList();
        Iterator var3 = params.entrySet().iterator();
        Map.Entry entry;
        while(var3.hasNext()) {
            entry = (Map.Entry)var3.next();
            signParams.add(new BasicNameValuePair((String) entry.getKey(), (String) entry.getValue()));
        }

        Collections.sort(signParams, new Comparator<BasicNameValuePair>() {

            @Override
            public int compare(BasicNameValuePair lhs, BasicNameValuePair rhs) {
                return lhs.getName().compareTo(rhs.getName());
            }
        });

        String signString = "";
        signString = urlToString(signParams);
        return MD5Util.encode(signString);
    }

    private static String urlToString(List<BasicNameValuePair> params){
        StringBuilder result = new StringBuilder();
        Iterator var3 = params.iterator();

        BasicNameValuePair item;
        while(var3.hasNext()) {
            item = (BasicNameValuePair) var3.next();
            if(result.length() > 0) {
                result.append("&");
            }

            result.append(item.getName());
            result.append("=");
            result.append(item.getValue());
        }
        return result.toString();
    }

    public static String noNeedPostParams(){
        StringBuffer sb = new StringBuffer();
        sb.append("&");
        if (!TextUtils.isEmpty(ComAPIManager.getComAPIManager().getSystemAPI()
                .getChannel())) {
            sb.append("channel=");
            sb.append(Uri.encode(ComAPIManager.getComAPIManager().getSystemAPI()
                    .getChannel()));
            sb.append("&");
        }
        if (!TextUtils.isEmpty(ComAPIManager.getComAPIManager().getSystemAPI()
                .getResId())) {
            sb.append("resid=");
            sb.append(Uri.encode(ComAPIManager.getComAPIManager().getSystemAPI()
                    .getResId()));
            sb.append("&");
        }

        if (!TextUtils.isEmpty(ComAPIManager.getComAPIManager().getSystemAPI()
                .getAppVersion())) {
            sb.append("sv=");
            sb.append(Uri.encode(ComAPIManager.getComAPIManager().getSystemAPI()
                    .getAppVersion()));
            sb.append("&");
        }
        if (!TextUtils.isEmpty(ComAPIManager.getComAPIManager().getSystemAPI()
                .getDeviceName())) {
            sb.append("mb=");
            sb.append(Uri.encode(ComAPIManager.getComAPIManager().getSystemAPI()
                    .getDeviceName()));
            sb.append("&");
        }
        if (!TextUtils.isEmpty(APIProxy.sys().getCuid())) {
            sb.append("cuid=");
            sb.append(Uri.encode(APIProxy.sys().getCuid()));
            sb.append("&");
        }
        if (!TextUtils.isEmpty(APIProxy.sys().getOsName())) {
            sb.append("os=");
            sb.append(Uri.encode(APIProxy.sys().getOsName()));
            sb.append("&");
        }
        sb.append("from=2");
        sb.append("&");
        sb.append("da_trd=carpool");
        sb.append("&");
        sb.append("da_model=plugin_carpool");

        return sb.toString();
    }
}
